package com.xcx.robot.mirai.factory.facade;

import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.util.SenderHelper;
import com.xcx.robot.util.FreemarkerUtils;
import com.xcx.robot.util.ImageUtils;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.util.*;

/**
 * @Author: 邢晨旭
 * @Date: 2021/6/22 09:33
 * @Description:
 */
@Service
@Slf4j
public class AnnounceFacade implements BaseFacade {
    public KeyWord keyWord = KeyWord.ANNOUNCE;


    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        try {
            //https://jx3.xoyo.com/announce/
            Document announce = Jsoup.connect("https://jx3.xoyo.com/announce?"+new Random().nextInt(100)).get();
            Elements announces = announce.getElementsByAttributeValueContaining("title", "版本更新公告");
            Document document = Jsoup.connect("https://jx3.xoyo.com/"+announces.get(0).attr("href")).get();
            Element detail_con = document.getElementsByClass("detail_con").get(0);
            Elements p = detail_con.getElementsByTag("p");
            List<String> list = new ArrayList<>();
            for (Element item : p) {
                list.add(item.text());
            }
            Map<String, Object> result = new HashMap<>();
            result.put("list", list);
            BufferedImage bufferedImage = FreemarkerUtils.turnImage("announce.ftl", result);
            SenderHelper.sendMessage(receiver, receiver.uploadImage(ImageUtils.initImage(bufferedImage)));
        } catch (Exception e) {
            log.error("公告查询报错{}", e);
        }

    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
